//
//  SXVideoDetailListViewController.m
//  ShiXun
//
//  Created by lanou on 15/10/26.
//  Copyright © 2015年 张琪. All rights reserved.
//

#import "SXVideoDetailListViewController.h"
#import "SXRequestManager.h"
#import "SXDataBaseManager.h"
#import "SXVideoDetailListTableViewCell.h"
#import "SXVideoDetailViewController.h"

@interface SXVideoDetailListViewController () <UITableViewDataSource, UITableViewDelegate>

@end

@implementation SXVideoDetailListViewController

- (UITableView *)mainTableView
{
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64)];
        _mainTableView.dataSource = self;
        _mainTableView.delegate = self;
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainTableView.showsVerticalScrollIndicator = NO;
        [_mainTableView registerClass:[SXVideoDetailListTableViewCell class] forCellReuseIdentifier:@"SXVideoDetailListTableViewCell"];
    }
    return _mainTableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _start = 0;
    _limit = 10;
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationItem.title = _name;
    
    [self requestData];
}

// 请求数据
- (void)requestData
{
    NSString *urlString = [kVideoDetailListURlString stringByAppendingString:[_name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    if (_start != 0) {
        urlString = [urlString stringByAppendingString:[NSString stringWithFormat:@"&start=%ld", _start]];
    }
    
    [SXRequestManager requestWithType:GET urlString:urlString parDic:nil finish:^(NSData *data) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers) error:nil];
        NSArray *listArray = dict[@"videoList"];
        if (_start == 0) {
            [self.dataSourceArray removeAllObjects];
        }
        for (NSDictionary *detailDict in listArray) {
            SXVideoDetailModel *model = [[SXVideoDetailModel alloc] init];
            [model setValuesForKeysWithDictionary:detailDict];
            model.dataDict = detailDict;
            [self.dataSourceArray addObject:model];
        }
        [self.mainTableView reloadData];
        [self removeLoadingView];
        
    } error:^(NSError *error) {
        NSLog(@"影音主页面请求数据失败%@", error);
        [self removeLoadingView];
    }];
}

// cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArray.count;
}

// cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SXVideoDetailListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SXVideoDetailListTableViewCell" forIndexPath:indexPath];
    
    SXVideoDetailModel *model = self.dataSourceArray[indexPath.row];
    cell.configureModel = model;
    
    cell.detailImageView.y = - (cell.y - self.mainTableView.contentOffset.y) * 60 / (kScreenHeight - 64 - cell.height);
    
    return cell;
}

// cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [SXVideoDetailListTableViewCell heightForCell];
}

// 滑动动画
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat scrollViewY = scrollView.contentOffset.y;
    // 判断是否被拖拽
    if (scrollView.isDragging) {
        if (scrollViewY > -50 && scrollViewY < 0) {
            _loadingTitleLabel.text = @"用力一点就可以刷新了";
        }
        if (scrollViewY < - 50) {
            _loadingTitleLabel.text = @"松开手就可以刷新了";
        }
    }
    
    NSArray *visibleCell = [self.mainTableView visibleCells];
    for (SXVideoDetailListTableViewCell *cell in visibleCell) {
        cell.detailImageView.y = - (cell.y - self.mainTableView.contentOffset.y) * 60 / (kScreenHeight - 64 - cell.height);
    }
}

// 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SXVideoDetailViewController *detailVC = [[SXVideoDetailViewController alloc] init];
    detailVC.detailModel = self.dataSourceArray[indexPath.row];
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
